package org.study.bookmanager.controller;

import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.study.bookmanager.io.reponse.RestBean;
import org.study.bookmanager.io.request.Book.AddBookReq;
import org.study.bookmanager.io.request.Book.AddBookTypeReq;
import org.study.bookmanager.io.request.Book.GetBooksReq;
import org.study.bookmanager.io.request.Book.UpdateBookReq;
import org.study.bookmanager.service.BookService;

@RestController//包含了@ResponseBody
@RequestMapping(value = "/v1/book",produces = "application/json;charset=UTF-8")
public class BookController {
    @Resource
    private BookService bookService;

    @PostMapping("/type")
    String addBookType(AddBookTypeReq req){
        if(req.getTitle()==null){
            return RestBean.handlerError(RestBean.Code.请填写图书标题);
        }
        if(req.getIsbn()==null){
            return RestBean.handlerError(RestBean.Code.请填写ISBN);
        }
        Object resp=bookService.addBookType(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }

    @PostMapping("/")
    String addBook(AddBookReq req){
        if(req.getIsbn()==null){
            return RestBean.handlerError(RestBean.Code.请填写ISBN);
        }
        if(req.getBarcodes()==null){
            return RestBean.handlerError(RestBean.Code.请填写条形码);
        }
        Object resp=bookService.addBook(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }

    @DeleteMapping("/")
    String deleteBook(@RequestParam int barcodeId){
        Object resp=bookService.deleteBook(barcodeId);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }

    @PutMapping("/")
    String updateBook(@RequestBody UpdateBookReq req){
        if(req.getTitle()==null){
            return RestBean.handlerError(RestBean.Code.请填写图书标题);
        }
        if(req.getIsbn()==null){
            return RestBean.handlerError(RestBean.Code.请填写ISBN);
        }
        Object resp=bookService.updateBook(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }

    @PostMapping(value = "/list")
    String findBooks(@RequestBody GetBooksReq req){
        Object resp=bookService.findBooks(req);
        if(RestBean.handlerError(resp)!=null){
            return RestBean.handlerError(resp);
        }
        return RestBean.success(resp);
    }
}
